<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\Api\ChatRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use SwooleTW\Http\Websocket\Facades\Websocket;

class ChatController extends Controller
{
    public function login(Request $request)
    {
        $user_id = $request->get('user_id');

        $fd = \Cache::get('fd:' . $this->user->id);

        if (!$fd) {
            return response()->json(['message' => 'fd 不存在 请重新建立连接', 'code' => 0, 'status' => false], 200);
        }

        if ($user_id == $this->user->id) {
            return response()->json(['message' => '你已经在该聊天室中', 'code' => 0, 'status' => false], 200);
        }

        $res = Websocket::setSender($fd)->join('uid_' . $user_id)->to('uid_' . $user_id)->emit('message', $this->user->nick_name . '加入了聊天室');

        if ($res) {
            return response()->json(['message' => trans('api.login_succeed'), 'code' => 1, 'status' => true, 'data' => ''], 200);
        } else {
            return response()->json(['message' => trans('api.login_failed'), 'code' => 0, 'status' => false], 200);
        }
    }

    public function sendMsg(ChatRequest $request)
    {
        $user_id = $request->get('user_id');

        $content_type = $request->get('content_type');

        $content = $this->translateContent($request->get('content'), $content_type);

        $res = Websocket::broadcast()->to('uid_' . $user_id)->emit('message', $content);

        if ($res) {
            return response()->json(['message' => trans('api.deliver_succeed'), 'code' => 1, 'status' => true, 'data' => ''], 200);
        } else {
            return response()->json(['message' => trans('api.deliver_failed'), 'code' => 0, 'status' => false], 200);
        }
    }

    public function logout(Request $request)
    {
        $user_id = $request->get('user_id');

        $fd = \Cache::get('fd:' . $this->user->id);

        if (!$fd) {
            return response()->json(['message' => 'fd 不存在 请重新建立连接', 'code' => 0, 'status' => false], 200);
        }

        Websocket::broadcast()->to('uid_' . $user_id)->emit('message', $this->user->nick_name . '退出了聊天室');

        $res = Websocket::setSender($fd)->leave('uid_' . $user_id);

        if ($res) {
            return response()->json(['message' => trans('api.logout_succeed'), 'code' => 1, 'status' => true, 'data' => ''], 200);
        } else {
            return response()->json(['message' => trans('api.logout_failed'), 'code' => 0, 'status' => false], 200);
        }
    }

    protected function translateContent($content, $type)
    {
        $result = [];

        if ($type == 'Msg') {
            $result['type'] = 'Msg';
            $result['content'] = $content;
        }

        if ($type == 'ImgMsg') {
            $result['type'] = 'ImgMsg';
            $result['content'] = image_url($content);
        }

        $result['user_id'] = $this->user->id;
        $result['user_nick_name'] = $this->user->nick_name;
        $result['user_avatar'] = image_url($this->user->avatar);
        $result['created_at'] = now()->toDateTimeString();

        return json_encode($result);
    }
}
